<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="javax.portlet.*"%>
<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState" %>

<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>


<portlet:defineObjects />


<script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">

<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
 
<link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.1/css/jquery.dataTables.css" />
<script src="http://cdn.datatables.net/1.10.1/js/jquery.dataTables.min.js" type="text/javascript"></script>


<table>
  <tr>
    <td class="iconSpacer" id ="searchMContainer" style="background-color: #028AC2;">
    	<a id="searchM" class="activ">
			<img title="Search" id="searchIcon" class="icons" src="<%= request.getContextPath()%>/html/image/search.png" width="60" height="60" />
		</a>
	</td>
    <td class="iconSpacer" id="schutzerMContainer">
    	<a id="schutzerM" class="activ">
			<img title="Take contact with the security officer" id="searchIcon" class="icons" src="<%= request.getContextPath()%>/html/image/schutzer.png" width="100" height="100" />
		</a>
	</td>
  </tr>
</table>

<hr width="100%" size="5">

<div id = "datenZugangMenu">

<table>
   <tr>
    <th>Search</th>
     <th>Histories (Last 20 Entries)</th>
  </tr>
  <tr>
    <td>
    	<div style="height: 350px;">
		    <aui:form name="fm" action="#" id="fm" method="post">
			<aui:row>
				<aui:column>
					<aui:input name="Message-id" label="MessageID" type="text" id="messageId"></aui:input>
				</aui:column>
		
				<aui:column>
					<aui:select name="Event-action" label="Event-action" id="eventAction">
						<aui:option value=""><liferay-ui:message key="Whatever" /></aui:option>
						<aui:option value="C"><liferay-ui:message key="Create" /></aui:option>
						<aui:option value="R"><liferay-ui:message key="Read/View/Print/Query" /></aui:option>
						<aui:option value="U"><liferay-ui:message key="Update" /></aui:option>
						<aui:option value="D"><liferay-ui:message key="Delete" /></aui:option>
						<aui:option value="E"><liferay-ui:message key="Execute" /></aui:option>
					</aui:select>	
				</aui:column>
		
			</aui:row>
			
			<aui:row>
				<aui:column>
					<aui:input name="Event-date (at)" label="From"  type="text" id="eventDateTimeAt" cssClass="picker"></aui:input>
				</aui:column>
		
				<aui:column>
					<aui:input name="Event-date (down)" label="To" type="text" id="eventDateTimeDown" cssClass="picker"></aui:input>
				</aui:column>
			</aui:row>
			
			<aui:row>
				<aui:column>
					<aui:input name="Username" label="Username" type="text" id="username"></aui:input>
				</aui:column>
				
				<aui:column>
					<aui:input name="Object" label="Name of Document" type="text" id="participantsObjectName"></aui:input>
				</aui:column>
			</aui:row>
			
			<aui:button type="reset" value="Clear"/> 
			<aui:button name="searchButton" onClick="suche()" type="button" value="Search" />
			
			</aui:form>
		</div>
    </td>
    <td>
    	<div id="chronik" style="height: 320px; width:200px; overflow:auto; border: 1px solid #ccc">
    		<jsp:include page="chronik.jsp"></jsp:include>
    	</div>
    </td>
  </tr>
</table>

<hr width="100%" size="5">

<div id="resultTable"></div>

</div>

<jsp:include page="datenschutzbeauftragter.jsp"></jsp:include>

<script type="text/javascript">
var messageId;
var eventDateTimeAt; 
var eventDateTimeDown;
var participantsObjectName; 
var eventAction;
var username; 

function suche (){
	messageId = document.getElementById("<portlet:namespace/>messageId").value.trim();
	eventDateTimeAt = document.getElementById("<portlet:namespace/>eventDateTimeAt").value.trim(); 
	eventDateTimeDown = document.getElementById("<portlet:namespace/>eventDateTimeDown").value.trim();
	participantsObjectName = document.getElementById("<portlet:namespace/>participantsObjectName").value.trim();
	eventAction = document.getElementById('<portlet:namespace/>eventAction').value.trim();
	username = document.getElementById("<portlet:namespace/>username").value.trim(); 
	
	if(eventDateTimeAt != "" && checkDate1(eventDateTimeAt) == false){
		alert("Your date is not conform. PLease enter a date in this form yyyy-mm-dd");
		return false;
	}
	if(eventDateTimeDown != "" && checkDate1(eventDateTimeDown) == false){
		alert("Your date is not conform. PLease enter a date in this form yyyy-mm-dd");
		return false;
	}
	
	if(checkDate2(eventDateTimeAt, eventDateTimeDown) == false){
		alert("The Date at 'From' can't be the same/greater than the date at 'To'");
		return false;
	}
	var arr = messageId.split(";");
	for (var i = 0; i < arr.length; i++){
		if(isNaN(arr[i])){
			alert("Please Enter only a number for MessageID!!!");
			return false;
		}
	}
	
    if (messageId == "" && eventDateTimeAt == "" && eventDateTimeDown == "" && participantsObjectName == "" && eventAction == "" && username == ""){
    	alert("Please fill out the form!!!");
      	return false;
    }
	var url = "<portlet:actionURL windowState='<%= LiferayWindowState.EXCLUSIVE.toString() %>'><portlet:param name='ajaxAction' value='searchData'></portlet:param></portlet:actionURL>";
		 $.post(
         		url,
         		{
          			"<portlet:namespace/>messageId" : messageId,
					"<portlet:namespace/>eventDateTimeAt" : eventDateTimeAt,
					"<portlet:namespace/>eventDateTimeDown" : eventDateTimeDown,
					"<portlet:namespace/>participantsObjectName" : participantsObjectName,
					"<portlet:namespace/>eventAction" : eventAction, 
					"<portlet:namespace/>username" : username,  
					"<portlet:namespace/>mailBody" : "", 
         		}, 
	           	function(data){
	               	$("#resultTable").html(data);
	                document.getElementById("<portlet:namespace/>messageId").value = "";
	       			document.getElementById("<portlet:namespace/>eventDateTimeAt").value = "";
	       			document.getElementById("<portlet:namespace/>eventDateTimeDown").value = "";
	       			document.getElementById("<portlet:namespace/>participantsObjectName").value = "";
	       			document.getElementById("<portlet:namespace/>username").value = "";
	       			document.getElementById('<portlet:namespace/>eventAction').options.selectedIndex = 0;
	       			loadChronik();
	          	})
}

function del (id){
	
	var url = "<portlet:actionURL windowState='<%= LiferayWindowState.EXCLUSIVE.toString() %>'><portlet:param name='ajaxAction' value='del'></portlet:param></portlet:actionURL>";
		 $.post(
         		url,
         		{
          			"<portlet:namespace/>messageId" : id,
					"<portlet:namespace/>eventDateTimeAt" : "",
					"<portlet:namespace/>eventDateTimeDown" : "",
					"<portlet:namespace/>participantsObjectName" : "",
					"<portlet:namespace/>eventAction" : "", 
					"<portlet:namespace/>username" : "",
					"<portlet:namespace/>mailBody" : "", 
         		}, 
	           	function(data){
	               	window.location.reload(true);
	          	})
}

function loadChronik (){
	
	var url = "<portlet:actionURL windowState='<%= LiferayWindowState.EXCLUSIVE.toString() %>'><portlet:param name='ajaxAction' value='chronik'></portlet:param></portlet:actionURL>";
		 $.post(
         		url,
         		{
          			"<portlet:namespace/>messageId" : "",
					"<portlet:namespace/>eventDateTimeAt" : "",
					"<portlet:namespace/>eventDateTimeDown" : "",
					"<portlet:namespace/>participantsObjectName" : "",
					"<portlet:namespace/>eventAction" : "", 
					"<portlet:namespace/>username" : "",
					"<portlet:namespace/>mailBody" : "", 
         		}, 
	           	function(data){
         			$("#chronik").html(data);
	          	})
}

function sendMail(){
	messageId = document.getElementById("<portlet:namespace/>messageIda").value.trim();
	username = document.getElementById("<portlet:namespace/>usernamea").value.trim();
	mailBody = document.getElementById("<portlet:namespace/>mailBody").value.trim();
	
	var arr = messageId.split(";");
	for (var i = 0; i < arr.length; i++){
		if(isNaN(arr[i])){
			alert("Please Enter only a number for MessageID!!!");
			return false;
		}
	}
	if (messageId == "" && username == ""){
  		alert("Please fill out the form!!!");
  		return false;
	}
	var url = "<portlet:actionURL windowState='<%= LiferayWindowState.EXCLUSIVE.toString() %>'><portlet:param name='ajaxAction' value='sendMail'></portlet:param></portlet:actionURL>";
	 $.post(
      		url,
      		{
       			"<portlet:namespace/>messageId" : messageId,
					"<portlet:namespace/>eventDateTimeAt" : "",
					"<portlet:namespace/>eventDateTimeDown" : "",
					"<portlet:namespace/>participantsObjectName" : "",
					"<portlet:namespace/>eventAction" : "", 
					"<portlet:namespace/>username" : username,  
					"<portlet:namespace/>mailBody" : mailBody, 
      		}, 
	           	function(data){
      		  	document.getElementById("<portlet:namespace/>messageIda").value = "";
     			document.getElementById("<portlet:namespace/>usernamea").value = "";
     			document.getElementById('<portlet:namespace/>mailBody').value = "";
     			
     			setTimeout("alert('Your Email was sent')",700);
     			
     			document.getElementById('searchMContainer').style.backgroundColor = "#028AC2";
     			document.getElementById('schutzerMContainer').style.backgroundColor = "#E4E4E4";
     			document.getElementById('datenschutzbeauftragterMenu').style.display = 'none';
     			document.getElementById('datenschutzbeauftragterMenu').style.visibility = 'hidden';
     			document.getElementById('datenZugangMenu').style.display = 'block';
     			document.getElementById('datenZugangMenu').style.visibility = 'visible';
	          	})
}

$(function() {
	$('.picker').datepicker({
		dateFormat: 'yy-mm-dd',
		firstDay: 1,
		monthNames: ['Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'],
		dayNames: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag','Samstag'],
		dayNamesMin: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'] , changeMonth: true, changeYear: true, maxDate: '0'
	});
	$( ".picker" ).datepicker();
});

$("#searchM").click(function(){
	document.getElementById('searchMContainer').style.backgroundColor = "#028AC2";
	document.getElementById('schutzerMContainer').style.backgroundColor = "#E4E4E4";
	document.getElementById('datenschutzbeauftragterMenu').style.display = 'none';
	document.getElementById('datenschutzbeauftragterMenu').style.visibility = 'hidden';
	document.getElementById('datenZugangMenu').style.display = 'block';
	document.getElementById('datenZugangMenu').style.visibility = 'visible';
});

$("#schutzerM").click(function(){
	document.getElementById('schutzerMContainer').style.backgroundColor = "#028AC2";
	document.getElementById('searchMContainer').style.backgroundColor = "#E4E4E4";
	document.getElementById('datenschutzbeauftragterMenu').style.display = 'block';
	document.getElementById('datenschutzbeauftragterMenu').style.visibility = 'visible';
	document.getElementById('datenZugangMenu').style.display = 'none';
	document.getElementById('datenZugangMenu').style.visibility = 'hidden';
});

function reloadChronik(username, messageId, eventAction, eventDateTimeAt, eventDateTimeDown, participantsObjectName){
	(messageId =="" ? document.getElementById("<portlet:namespace/>messageId").value = "" : document.getElementById("<portlet:namespace/>messageId").value = messageId);
	(participantsObjectName =="" ? document.getElementById("<portlet:namespace/>participantsObjectName").value = "" : document.getElementById("<portlet:namespace/>participantsObjectName").value = participantsObjectName);
	(username =="" ? document.getElementById("<portlet:namespace/>username").value = "" : document.getElementById("<portlet:namespace/>username").value = username);
	(eventDateTimeAt =="" ? document.getElementById("<portlet:namespace/>eventDateTimeAt").value = "" : document.getElementById("<portlet:namespace/>eventDateTimeAt").value = eventDateTimeAt);
	(eventDateTimeDown =="" ? document.getElementById("<portlet:namespace/>eventDateTimeDown").value = "" : document.getElementById("<portlet:namespace/>eventDateTimeDown").value = eventDateTimeDown);
	(eventAction =="" ? document.getElementById('<portlet:namespace/>eventAction').options.selectedIndex  = 0 : document.getElementById('<portlet:namespace/>eventAction').options.selectedIndex = setAction(eventAction));
}

function checkIP(ip){
	var reg = new RegExp('^(([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))\.){3}([01]?[0-9]?[0-9]|2([0-4][0-9]|5[0-5]))$', 'i');
	if(reg.test(ip)){
		return true;
	}
	else{
		if(js_checkEmail(ip) == true)
			return true
		else
			return false;
	}
}

/**
 *Diese Methode prüft, ob eine Email in der guten Form ist. Z.B: test@google.com
 * Gibt True zurück, falls es richtig ist 
 */
function js_checkEmail(text){
	var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}[a-z]{2,6}$', 'i');
	if(reg.test(text))
		return true;
	else
		return false;
}

function checkDate1(date){
	var reg = new RegExp('^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$', 'i');
	if(reg.test(date))
		return true;
	else
		return false;
}

function checkDate2(date1, date2){
	d1 = new Date(date1);
	d2 = new Date(date2);
	if(d1.getTime() > d2.getTime() || d1.getTime() == d2.getTime())
		return false;
	else
		return true;
}

function setAction(val){
	var action = 0;
	switch(val){
		case 'C':
			action = 1;
		break;
		case 'R':
			action = 2;
		break;
		case 'U':
			action = 3;
		break;
		case 'D':
			action = 4;
		break;
		case 'E':
			action = 5;
		break;	
	}
	return action;
}
</script>
